﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public DataRow Convert2ICC_HRSTAMM(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryICC_ABAX(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                case "JLT":
                default:
                    #region====ICC_HRSTAMM======
                    dwReturn["ROWNID"] = "0";
                    dwReturn["MANDANT"] = "00";
                    dwReturn["COMPANY"] = "   ";
                    dwReturn["USERID"] = "        ";
                    dwReturn["PWD"] = "'ITXV                ";
                    dwReturn["EXCELFILE"] = "xls";
                    dwReturn["DBSYSTEM"] = "intex1";
                    dwReturn["DBUSER"] = "intex";
                    dwReturn["DBPWD"] = "intex";
                    dwReturn["SQLDB"] = "Data Source=192.168.18.227;Initial Catalog=jinling;Persist Security Info=True;User ID=sa;   ";
                    dwReturn["SQLDBUSER"] = "sa  ";
                    dwReturn["SQLDBPWD"] = "sasa  ";
                    dwReturn["NETDRIVE"] = "D:/DS40";
                    dwReturn["ROOTDRIVE"] = " C:/IntexReport ";
                    dwReturn["BYAGENT"] = " YES";
                    dwReturn["UPDATER"] = " ";
                    dwReturn["IPADDRESS"] = "192.168.1.11";
                    dwReturn["IPADDRESS2"] = "80.166.130.110";
                    dwReturn["NEWUSER"] = "YCAV    ";
                    dwReturn["NEWTIME"] = "";
                    dwReturn["CHGUSER"] = " ";
                    dwReturn["CHGTIME"] = "0";

                    #endregion
                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwInput.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapICC_HRSTAMM(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveICC_HRSTAMM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;

            Result = QueryICC_HRSTAMM(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapICC_HRSTAMM(ref dwLocal, dwData);

            if (!flgExist)
            {
                dwLocal["ROWNID"] = GetSeq();
                InsertICC_HRSTAMM(dwLocal, ITXDBVERSION);
            }
            UpdateICC_HRSTAMM(dwLocal);
        }
        public void InsertICC_HRSTAMM(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "INSERT INTO ICC_HRSTAMM(MANDANT,ROWNID,COMPANY,USERID,PWD)"
                                + " VALUES (:MANDANT,:ROWNID,:COMPANY,:USERID,:PWD)";
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    or1.Parameters.Add("COMPANY", OracleType.NVarChar).Value = dwData["COMPANY"].ToString();
                    or1.Parameters.Add("USERID", OracleType.NVarChar).Value = dwData["USERID"].ToString();
                    or1.Parameters.Add("PWD", OracleType.NVarChar).Value = dwData["PWD"].ToString();
                    break;
                case 2:
                    or1.CommandText = "INSERT INTO ICC_HRSTAMM (MANDANT,ROWNID,COMPANY,USERID)"
                        + " SELECT HRMANDANT,SEQ_ICC_ITX.NEXTVAL AS SEQ,:COMPANY,HRBENID FROM HRSTAMM WHERE NOT EXISTS (SELECT 1 FROM ICC_HRSTAMM WHERE RTRIM(HRBENID)=RTRIM(USERID))";
                    or1.Parameters.Add("COMPANY", OracleType.NVarChar).Value = dwData["COMPANY"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }

        public DataSet QueryICC_HRSTAMM(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT * FROM ICC_HRSTAMM WHERE  ROWNID=:ROWNID"; ;
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT * FROM ICC_HRSTAMM WHERE  MANDANT=:MANDANT AND COMPANY=:COMPANY AND RTRIM(USERID)=:USERID "; ;
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    or1.Parameters.Add("COMPANY", OracleType.NVarChar).Value = dwData["COMPANY"].ToString();
                    or1.Parameters.Add("USERID", OracleType.NVarChar).Value = dwData["USERID"].ToString().TrimEnd();
                    break;
                case 3:
                    or1.CommandText = "SELECT 1 FROM ICC_HRSTAMM WHERE  ROWNID=COMPANY"; ;
                    break;
                case 9999:
                    or1.CommandText = "SELECT * FROM ICC_HRSTAMM";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "ICC_HRSTAMM";
            Return = Result;
            return Return;
        }
        public void DeleteICC_HRSTAMM(DataRow dwData, int option = 1)
        {
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM ICC_HRSTAMM WHERE  ROWNID=:ROWNID"; ;
                    or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
                    break;
                case 2:
                    or1.CommandText = "DELETE FROM ICC_HRSTAMM WHERE  ROWNID=COMPANY"; ;
                     break;
            }
            Connect2Proxy(or1);
        }
        public void UpdateICC_HRSTAMM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            #region====Command==
            or1.CommandText = "UPDATE ICC_HRSTAMM SET "
                + "MANDANT=:MANDANT,"
                + "COMPANY=:COMPANY,"
                + "USERID=:USERID,"
                + "PWD=:PWD,"
                + "EXCELFILE=:EXCELFILE,"
                + "DBSYSTEM=:DBSYSTEM,"
                + "DBUSER=:DBUSER,"
                + "DBPWD=:DBPWD,"
                + "SQLDB=:SQLDB,"
                + "SQLDBUSER=:SQLDBUSER,"
                + "SQLDBPWD=:SQLDBPWD,"
                + "NETDRIVE=:NETDRIVE,"
                + "ROOTDRIVE=:ROOTDRIVE,"
                + "CHECKFOLDER=:CHECKFOLDER,"
                + "BYAGENT=:BYAGENT,"
                + "UPDATER=:UPDATER,"
                + "IPADDRESS=:IPADDRESS,"
                + "IPADDRESS2=:IPADDRESS2,"
                + "NEWUSER=:NEWUSER,"
                + "NEWTIME=:NEWTIME,"
                + "CHGUSER=:CHGUSER,"
                + "CHGTIME=:CHGTIME "
                + " WHERE  "
                + "    ROWNID=:ROWNID ";
            #endregion
            #region====Parameters====
            or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
            or1.Parameters.Add("ROWNID", OracleType.NVarChar).Value = dwData["ROWNID"].ToString();
            or1.Parameters.Add("COMPANY", OracleType.NVarChar).Value = dwData["COMPANY"].ToString();
            or1.Parameters.Add("USERID", OracleType.NVarChar).Value = dwData["USERID"].ToString();
            or1.Parameters.Add("PWD", OracleType.NVarChar).Value = dwData["PWD"].ToString();
            or1.Parameters.Add("EXCELFILE", OracleType.NVarChar).Value = dwData["EXCELFILE"].ToString();
            or1.Parameters.Add("DBSYSTEM", OracleType.NVarChar).Value = dwData["DBSYSTEM"].ToString();
            or1.Parameters.Add("DBUSER", OracleType.NVarChar).Value = dwData["DBUSER"].ToString();
            or1.Parameters.Add("DBPWD", OracleType.NVarChar).Value = dwData["DBPWD"].ToString();
            or1.Parameters.Add("SQLDB", OracleType.NVarChar).Value = dwData["SQLDB"].ToString();
            or1.Parameters.Add("SQLDBUSER", OracleType.NVarChar).Value = dwData["SQLDBUSER"].ToString();
            or1.Parameters.Add("SQLDBPWD", OracleType.NVarChar).Value = dwData["SQLDBPWD"].ToString();
            or1.Parameters.Add("NETDRIVE", OracleType.NVarChar).Value = dwData["NETDRIVE"].ToString();
            or1.Parameters.Add("ROOTDRIVE", OracleType.NVarChar).Value = dwData["ROOTDRIVE"].ToString();
            or1.Parameters.Add("CHECKFOLDER", OracleType.NVarChar).Value = dwData["CHECKFOLDER"].ToString();
            or1.Parameters.Add("BYAGENT", OracleType.NVarChar).Value = dwData["BYAGENT"].ToString();
            or1.Parameters.Add("UPDATER", OracleType.NVarChar).Value = dwData["UPDATER"].ToString();
            or1.Parameters.Add("IPADDRESS", OracleType.NVarChar).Value = dwData["IPADDRESS"].ToString();
            or1.Parameters.Add("IPADDRESS2", OracleType.NVarChar).Value = dwData["IPADDRESS2"].ToString();
            or1.Parameters.Add("NEWUSER", OracleType.NVarChar).Value = dwData["NEWUSER"].ToString();
            or1.Parameters.Add("NEWTIME", OracleType.NVarChar).Value = dwData["NEWTIME"].ToString();
            or1.Parameters.Add("CHGUSER", OracleType.NVarChar).Value = dwData["CHGUSER"].ToString();
            or1.Parameters.Add("CHGTIME", OracleType.NVarChar).Value = dwData["CHGTIME"].ToString();
            #endregion
            Connect2Proxy(or1);
        }
    }
}
